//
//  ResultHeader.m
//  体育
//
//  Created by liujianlong on 2022/10/24.
//

#import "ResultHeader.h"
#import "DayCell.h"

@interface ResultHeader()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic)  NSArray *array;
@property (strong, nonatomic)  NSMutableArray *stampArray;
@property (assign, nonatomic)  NSInteger startStamp;
@end
@implementation ResultHeader
- (void)awakeFromNib{
    [super awakeFromNib];
}

- (void)setStamp:(NSInteger)stamp{
    _stamp = stamp;
    self.array = @[@"Su",@"Mo",@"Tu",@"We",@"Th",@"Fr",@"Sa"];
    self.startStamp = self.stamp;
    self.stampArray = [NSMutableArray array];
    
    NSDateComponents *date = [NSDate dateOfStamp:self.startStamp];
    
    [self.stampArray addObject:@(self.startStamp)];
    
    NSInteger weekday = [NSDate weakDay:[NSDate dateWithTimeIntervalSince1970:self.startStamp]];
    NSInteger startDay = date.day;
    
    for (NSInteger i = weekday; i < 7; i++) {
        NSInteger temp = self.startStamp + 86400 * (i + 1 - weekday);
        [self.stampArray addObject:@(temp)];
    }
    for (NSInteger i = weekday ; i > 1; i--) {
        NSInteger temp = self.startStamp - 86400 * (weekday + 1 - i);
        [self.stampArray insertObject:@(temp) atIndex:0];
    }
    [self.collectionView registerNib:[DayCell nib] forCellWithReuseIdentifier:[DayCell ID]];
}
#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DayCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[DayCell ID] forIndexPath:indexPath];
    int stamp = [self.stampArray[indexPath.row] intValue];
    cell.title.text = [NSString stringWithFormat:@"%@",self.array[indexPath.row]];
    cell.content.text =  [NSDate transToDate:@"dd" :stamp];
    cell.content.borderColor = self.stamp == stamp ? Hex(@"#2756CA"):[UIColor clearColor];
    cell.content.textHexColor = self.stamp == stamp ? @"#2756CA" : @"#333333";
    
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView reloadData];
    if (self.block) {
        self.stamp = [self.stampArray[indexPath.row] intValue];
        self.block(self.stamp);
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
  CGFloat w = (SCREEN_WIDTH - 40) / 7.0;
    return  CGSizeMake(w,70);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
